package zhang.jdbc.graph;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.zhang.management.ChengJiLuRu;
import com.zhang.management.KeChengGuanLi;
import com.zhang.management.XueShengChengJiDan;
import com.zhang.management.XueShengXuanKe;

import zhang.utils.JDBCImp;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.awt.event.ActionEvent;

public class JdbcGrap extends JFrame {

	/**一个简单的学生管理程序
	 * @author 张永赞
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField snameT;
	private JTextField sageT;
	private JTextField snoT;
	private JTextField sphone;
	private JDBCImp jdu=new JDBCImp();
	private JTextField ssexT;
	private Student stu_=null;
	private List<Student> list=null;
	private JLabel img;
	private JButton chakanchengji;
	private JButton xuanke;
	private JButton kechengguanli;
	private JButton chengjigunali;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JdbcGrap frame = new JdbcGrap();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public JdbcGrap(int role) {
		this();
		//role=0 是教师
		if(role==0) {
			xuanke.setEnabled(false);
			chakanchengji.setEnabled(false);
		}else {
			kechengguanli.setEnabled(false);
			chengjigunali.setEnabled(false);
			
		}
		
	}
	public JdbcGrap() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 780, 489);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		list = jdu.selectAll(Student.class);
		JButton insertButton = new JButton("\u63D2\u5165");
		//插入
		insertButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(snoT.getText().equals("")) {
					JOptionPane.showMessageDialog(null, "请输入学生信息！");
				}else {
					Student stu = new Student(Integer.parseInt(snoT.getText()),
							snameT.getText(), ssexT.getText(), Integer.parseInt(sageT.getText()),"", sphone.getText());
							String temp = img.toString().split("defaultIcon=")[1];
					        String iconPath = temp.substring(0, temp.indexOf(","));
					        stu.setSimage(iconPath);
					        stu_=stu;
					        jdu.insert(stu);
					        list.add(stu);
				}
				
			}
		});
		insertButton.setBounds(20, 401, 97, 23);
		contentPane.add(insertButton);
		//修改
		JButton updateButton = new JButton("\u4FEE\u6539");
		updateButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				list.remove(list.indexOf(stu_));
				Student stu = new Student(Integer.parseInt(snoT.getText()),
				snameT.getText(), ssexT.getText(), Integer.parseInt(sageT.getText()),"", sphone.getText());
				jdu.update(stu);
				list.add(stu);
			}
		});
		updateButton.setBounds(123, 401, 97, 23);
		contentPane.add(updateButton);
		
		JButton readButton = new JButton("\u67E5\u8BE2");
		//查询单个学生byid
		readButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Student stu = (Student) jdu.selectOne(Student.class, Integer.parseInt(snoT.getText()));
				stu_ =stu;
				showMessage(stu);
			}
		});
		readButton.setBounds(230, 401, 93, 23);
		contentPane.add(readButton);
		//通过学生的id删除学生
		JButton deleatButton = new JButton("\u5220\u9664");
		deleatButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Student stu = new Student(Integer.parseInt(snoT.getText()));
				jdu.delete(stu);
				list.remove(list.indexOf(stu_));
			}
		});
		deleatButton.setBounds(328, 401, 97, 23);
		contentPane.add(deleatButton);
		
		snameT = new JTextField();
		snameT.setBounds(106, 10, 169, 21);
		contentPane.add(snameT);
		snameT.setColumns(10);
		
		sageT = new JTextField();
		sageT.setBounds(106, 238, 169, 21);
		contentPane.add(sageT);
		sageT.setColumns(10);
		
		snoT = new JTextField();
		snoT.setBounds(106, 84, 169, 21);
		contentPane.add(snoT);
		snoT.setColumns(10);
		
		JLabel label = new JLabel("\u59D3\u540D");
		label.setBounds(31, 13, 54, 15);
		contentPane.add(label);
		
		JLabel lblNewLabel = new JLabel("\u5B66\u53F7");
		lblNewLabel.setBounds(31, 87, 54, 15);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("\u5E74\u9F84");
		lblNewLabel_1.setBounds(28, 241, 54, 15);
		contentPane.add(lblNewLabel_1);
		
		img = new JLabel("");
		img.setBounds(474, 55, 220, 301);
		img.setIcon(null);
		contentPane.add(img);
		img.setIcon(new ImageIcon("src\\img\\renwu.png"));
		JLabel label_1 = new JLabel("\u624B\u673A\u53F7");
		label_1.setBounds(31, 315, 54, 15);
		contentPane.add(label_1);
		
		sphone = new JTextField();
		sphone.setBounds(106, 312, 169, 21);
		contentPane.add(sphone);
		sphone.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("\u6027\u522B");
		lblNewLabel_3.setBounds(31, 164, 54, 15);
		contentPane.add(lblNewLabel_3);
		
		ssexT = new JTextField();
		ssexT.setBounds(106, 161, 169, 21);
		contentPane.add(ssexT);
		ssexT.setColumns(10);
		
		JButton last = new JButton("\u4E0A\u4E00\u4E2A");
		//查询上一个学生
		last.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(snoT.getText().equals("")) {
					JOptionPane.showMessageDialog(null, "请查询一个学生作为基准！");
				}else {
					showMessage(list.get((list.indexOf(stu_)-1+list.size())%list.size()));
					stu_=list.get((list.indexOf(stu_)-1+list.size())%list.size());
				}
			}
		});
		last.setBounds(20, 369, 97, 23);
		contentPane.add(last);
		//查询下一个学生
		JButton next = new JButton("\u4E0B\u4E00\u4E2A");
		next.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(snoT.getText().equals("")) {
					JOptionPane.showMessageDialog(null, "请查询一个学生作为基准！");
				}else {
					showMessage(list.get((list.indexOf(stu_)+1)%list.size()));
					stu_=list.get((list.indexOf(stu_)+1)%list.size());
				}
			}
		});
		next.setBounds(123, 369, 97, 23);
		contentPane.add(next);
		
		JButton random = new JButton("\u968F\u673A\u70B9\u540D");
		random.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				stu_=list.get((int)(Math.random()*list.size()));
				showMessage(stu_);
			}
		});
		random.setBounds(328, 369, 97, 23);
		contentPane.add(random);
		
		JButton pushimg = new JButton("\u4E0A\u4F20\u56FE\u7247");
		pushimg.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser jfc = new JFileChooser();
				jfc.showOpenDialog(null);
				File f=null;
				FileInputStream fis =null;
				FileOutputStream fos = null;
				try {
					f = jfc.getSelectedFile();
					fis = new FileInputStream(f);
					fos = new FileOutputStream("src\\img\\"+f.getName());
					int b;
					while((b=fis.read())!=-1) {
						fos.write(b);
					}
				} catch (Exception e1) {
					System.out.println("请选择文件！");
				}
				img.setIcon(new ImageIcon("src\\img\\"+f.getName()));
				
			}
		});
		pushimg.setBounds(546, 401, 97, 23);
		contentPane.add(pushimg);
		
		chengjigunali = new JButton("成绩管理");
		chengjigunali.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new ChengJiLuRu();
			}
		});
		chengjigunali.setBounds(433, 370, 97, 21);
		contentPane.add(chengjigunali);
		
		kechengguanli = new JButton("课程管理");
		kechengguanli.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new KeChengGuanLi();
			}
		});
		kechengguanli.setBounds(435, 401, 97, 23);
		contentPane.add(kechengguanli);
		
		chakanchengji = new JButton("查看成绩");
		chakanchengji.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new XueShengChengJiDan(stu_);
			}
		});
		chakanchengji.setBounds(546, 369, 97, 23);
		contentPane.add(chakanchengji);
		
		xuanke = new JButton("选课");
		xuanke.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Student s = stu_==null? new Student():stu_;
				new XueShengXuanKe(s);
			}
		});
		xuanke.setBounds(230, 369, 93, 23);
		contentPane.add(xuanke);
		this.setVisible(true);
	}
	void showMessage(Student stu) {
		snameT.setText(stu.getname());
		sageT.setText(stu.getSage()+"");
		snoT.setText(stu.getSid()+"");
		sphone.setText(stu.getSiphone());
		ssexT.setText(stu.getSsex());
	}
}
